package com.rongshu.view.home.presenter;

import android.content.Context;

import com.rongshu.api.RetorfitService;
import com.rongshu.view.base.mvp.BasePresenter;
import com.rongshu.view.base.mvp.INetBean;
import com.rongshu.view.home.contract.CommentDetailsView;
import com.rongshu.view.home.entity.CommenOneAllDetails;
import com.rongshu.view.home.entity.CommentDetails;
import com.rongshu.view.home.entity.CommentReplysBean;
import com.rongshu.view.home.entity.ContentClass;

import java.util.List;

import okhttp3.ResponseBody;
import rx.Subscriber;
import rx.functions.Func1;

public class CommentDetailsPresenter extends BasePresenter<CommentDetailsView> {

    public CommentDetailsPresenter(Context context) {
        super(context);
    }


    /**
     * 获取文章详情下的评论
     */
    public void getCommentDetail(int user_id, int contribute_id, int Sort,
                                 int pageIndex, int pageSize, String sign) {
        RetorfitService.getcommentDetail
                (user_id, contribute_id, Sort,
                        pageIndex, pageSize, sign)
                .map(new Func1<CommentDetails, List<CommentDetails.ResponseBean>>() {
                    @Override
                    public List<CommentDetails.ResponseBean> call(CommentDetails commentDetails) {
                        return commentDetails.getResponse();
                    }
                })
                .subscribe(new Subscriber<List<CommentDetails.ResponseBean>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(List<CommentDetails.ResponseBean> responseBeanList) {
                        if (getView() != null) {
                            getView().onCommentMsg(responseBeanList);
                        }
                    }
                });
    }

    /**
     * 获取单个评论下的所有回复
     */
    public void getOnecommentDetail(int user_id, int comment_id, int pageIndex, int pageSize, String sign) {
        RetorfitService.getOnecommentDetail(user_id, comment_id, pageIndex, pageSize, sign)
                .map(new Func1<CommenOneAllDetails, List<CommentReplysBean>>() {
                    @Override
                    public List<CommentReplysBean> call(CommenOneAllDetails commenOneAllDetails) {
                        return commenOneAllDetails.getComment_replys();
                    }
                })
                .map(new Func1<List<CommentReplysBean>, List<CommentReplysBean>>() {
                    @Override
                    public List<CommentReplysBean> call(List<CommentReplysBean> commentReplysBeans) {
                        for (CommentReplysBean commentReplysBean : commentReplysBeans) {
                            commentReplysBean.setAvatar(commentReplysBean.getFrom_avatar());
                            commentReplysBean.setVip(commentReplysBean.isFrom_vip());
                        }
                        return commentReplysBeans;
                    }
                })
                .subscribe(new Subscriber<List<CommentReplysBean>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(List<CommentReplysBean> commentReplysBeans) {
                        if (getView() != null) {
                            getView().onOnecommentDetail(commentReplysBeans);
                        }
                    }
                });
    }

    /**
     * 文章评论/评论回复/回复回复
     */
    public void postCommentOrReplyAdd(int user_id, int contribute_id, int comment_id,
                                      int reply_id, String sign, ContentClass contentClass) {
        RetorfitService.postCommentOrReplyAdd(user_id, contribute_id, comment_id, reply_id, sign, contentClass)
                .subscribe(new Subscriber<ResponseBody>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(ResponseBody body) {
                        if (getView() != null) {
                            getView().onPostCommentOrReplyAdd(body);
                        }
                    }
                });
    }

    /**
     * 评论点赞（任何评论）
     */
    public void getCommentlike(int user_id, int type, int obj_id, int comment_id, String sign) {
        RetorfitService.getCommentlike(user_id, type, obj_id, comment_id, sign)
                .subscribe(new Subscriber<INetBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(INetBean iNetBean) {
                        if (getView() != null) {
                            getView().onCommentlike(iNetBean);
                        }
                    }
                });
    }
}
